package lk.icta.core.service.impl;

import lk.icta.common.dao.impl.BaseDAO;
import lk.icta.common.service.DocumentManagementService;
import lk.icta.core.dao.GOInterestInformationDAO;
import lk.icta.core.entity.GOInterestInformation;
import lk.icta.core.service.GOInterestManagementService;
import lk.icta.user.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Tharanga Punchihewa
 */
public class GOInterestManagementServiceImpl extends BaseDAO implements GOInterestManagementService {

    private static final Logger logger = LoggerFactory.getLogger(CalenderScheduleManagementServiceImpl.class);
    private final GOInterestInformationDAO goInterestInformationDAO;
    private final DocumentManagementService documentManagementService;

    public GOInterestManagementServiceImpl(GOInterestInformationDAO goInterestInformationDAO, DocumentManagementService documentManagementService) {
        this.goInterestInformationDAO = goInterestInformationDAO;
        this.documentManagementService = documentManagementService;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void addGOInterestInformation(GOInterestInformation goInterestInformation, User user) {
        goInterestInformation.setStatus(GOInterestInformation.State.DATA_ENTRY);
        goInterestInformation.setActive(true);
        goInterestInformation.setOrganizationId(user.getOrganization().getId());
        goInterestInformationDAO.addGOInterestInformation(goInterestInformation, user);
        if (goInterestInformation.getDocumentList() != null) {
            try {
                logger.debug("Uploading files");
                documentManagementService.uploadfiles(user, goInterestInformation.getDocumentList(), goInterestInformation.getId());
            } catch (Exception e) {
                logger.debug("Error :{}", e);
            }
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void updateGOInterestInformation(GOInterestInformation goInterestInformation, User user) {
        goInterestInformation.setStatus(GOInterestInformation.State.DATA_ENTRY);
        goInterestInformationDAO.updateGOInterestInformation(goInterestInformation, user);
        if (goInterestInformation.getDocumentList() != null) {
            try {
                logger.debug("Uploading files");
                documentManagementService.uploadfiles(user, goInterestInformation.getDocumentList(), goInterestInformation.getId());
            } catch (Exception e) {
                logger.debug("Error :{}", e);
            }
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public GOInterestInformation getGOInterestInformationById(long goInterestInformationId, User user) {
        return goInterestInformationDAO.getGOInterestInformationById(goInterestInformationId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public List<GOInterestInformation> getGOInterestInformationListToBeEdit(User user) {
        //return goInterestInformationDAO.getGOInterestInformationListByState(GOInterestInformation.State.DATA_ENTRY);
        return goInterestInformationDAO.getGOInterestInformationListToBeEdited(user);
    }


    @Transactional(propagation = Propagation.REQUIRED)
    public List<GOInterestInformation> getGOInterestInformationListToBeApproved(User user) {
        return goInterestInformationDAO.getGOInterestInformationListByState(GOInterestInformation.State.DATA_ENTRY);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void approveGOInterestInformation(GOInterestInformation goInterestInformation, User user) {
        logger.debug("Approving a GO Information by : {}", user.getUsername());
        goInterestInformation.setStatus(GOInterestInformation.State.APPROVED);
        goInterestInformationDAO.updateGOInterestInformation(goInterestInformation, user);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void rejectGOInterestInformation(GOInterestInformation goInterestInformation, User user) {
        logger.debug("Rejecting a GO Information by : {}", user.getUsername());
        goInterestInformation.setStatus(GOInterestInformation.State.REJECTED);
        goInterestInformationDAO.updateGOInterestInformation(goInterestInformation, user);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public List<GOInterestInformation> getGOInterestInformationList(User user) {
        return goInterestInformationDAO.getGOInterestInformationList(user);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void activeGOInterestInformation(GOInterestInformation goInterestInformation, User user) {
        goInterestInformation.setActive(true);
        goInterestInformationDAO.updateGOInterestInformation(goInterestInformation, user);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void inactiveGOInterestInformation(GOInterestInformation goInterestInformation, User user) {
        goInterestInformation.setActive(false);
        goInterestInformationDAO.updateGOInterestInformation(goInterestInformation, user);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public List<GOInterestInformation> getGOInterestInformationListByOrganizationId(User user) {
        return goInterestInformationDAO.getGOInterestInformationListByOrganizationId(user);
    }
}
